/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//ResultSetGenericModelBinder binder = new ResultSetGenericModelBinder();
//ArrayList<ImportCar> items = binder.BindModels(results);
package Prototypes;

import ModelLayer.Customer;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Moulde
 */
public class CustomerResultSetModelBinder
{
    public CustomerResultSetModelBinder()
    {
    }
    
    public Customer BindModel(ResultSet result) throws SQLException
    {
        String[] columnNames = GetColumnNames(result);
        Field[] fields = Customer.class.getDeclaredFields();
        
        while (result.next())
        {
            Customer person = new Customer();
            
            for (String columnName : columnNames)
            {
                try
                {
                    boolean isPresent = false;
                    for (Field field : fields)
                    {
                        if (field.getName().equals(columnName))
                        {
                            isPresent = true;
                            break;
                        }
                    }

                    if (isPresent)
                    {
                        Field field = Customer.class.getDeclaredField(columnName);
                        field.setAccessible(true);
                        Type currentType = field.getType();

                        if (currentType == Integer.class)
                        {
                            field.setInt(person, result.getInt(columnName));
                        } else if (currentType == String.class)
                        {
                            field.set(person, result.getString(columnName));
                        } else if (currentType == double.class || currentType == Double.class)
                        {
                            field.set(person, result.getBigDecimal(columnName).doubleValue());
                        } else
                        {
                            field.set(person, result.getObject(columnName));
                        }
                    }
                }



                catch (IllegalAccessException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (IllegalArgumentException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (NoSuchFieldException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (SecurityException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            return person;
        }
        
        return new Customer();
    }

    private Customer BindInternalModel(ResultSet result) throws SQLException
    {
        Customer person = new Customer();
        return person;
    }

    public ArrayList<Customer> BindModels(ResultSet result) throws SQLException
    {
        ArrayList<Customer> employees = new ArrayList<Customer>();

        String[] columnNames = GetColumnNames(result);
        Field[] fields = Customer.class.getDeclaredFields();

        while (result.next())
        {
            Customer employee = new Customer();

            for (String columnName : columnNames)
            {
                try
                {
                    boolean isPresent = false;
                    for (Field field : fields)
                    {
                        if (field.getName().equals(columnName))
                        {
                            isPresent = true;
                            break;
                        }
                    }

                    if (isPresent)
                    {
                        Field field = Customer.class.getDeclaredField(columnName);
                        field.setAccessible(true);
                        Type currentType = field.getType();

                        if (currentType == Integer.class)
                        {
                            field.setInt(employee, result.getInt(columnName));
                        } else if (currentType == String.class)
                        {
                            field.set(employee, result.getString(columnName));
                        } else if (currentType == double.class || currentType == Double.class)
                        {
                            field.set(employee, result.getBigDecimal(columnName).doubleValue());
                        } else
                        {
                            field.set(employee, result.getObject(columnName));
                        }
                    }
                }



                catch (IllegalAccessException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (IllegalArgumentException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (NoSuchFieldException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (SecurityException ex)
                {
                    Logger.getLogger(CustomerResultSetModelBinder.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            employees.add(employee);
        }

        return employees;
    }

    public String[] GetColumnNames(ResultSet results) throws SQLException
    {
        ResultSetMetaData metaData = results.getMetaData();
        int columnCount = metaData.getColumnCount();
        String[] columns = new String[columnCount];

        for (int i = 0; i < columnCount; i++)
        {
            columns[i] = metaData.getColumnName(i + 1);
        }

        return columns;
    }
}
